import React from 'react';
import { FormGroup, DATA_ENTRY } from '../../../../../comps';
import PropTypes from 'prop-types';
import { message, Button } from 'antd';
import { connect } from 'dva';
import moment from 'moment';
class ZcBaseInfo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      baseInfoRef: undefined,
      startDate: undefined,
      endDate: undefined,
      rateType: undefined,
      mainRatingDepartData: undefined, //主体信用评级机构
      debtRatingDepartData: undefined, //债项信用评级机构
    };
  }

  componentDidMount() {
    this.props?.onRef && this.props?.onRef(this);
    const { baseInfo = {} } = this.props;
    this.setState({
      startDate: baseInfo.startDate,
      endDate: baseInfo.endDate,
      assetType: baseInfo.assetType,
    });
    //主体信用评级机构
    this.props.dispatch({
      type: 'nonstandarmaintenanceNewM/getMainRatingDepartData',
      payload: {},
      callback: (type, res) => {
        if (type == 'success') {
          this.setState({
            mainRatingDepartData: res && res.dataSource,
          });
        }
      },
    });
    //债项信用评级机构
    this.props.dispatch({
      type: 'nonstandarmaintenanceNewM/getDebtRatingDepartData',
      payload: {},
      callback: (type, res) => {
        if (type == 'success') {
          if (type == 'success') {
            this.setState({
              debtRatingDepartData: res && res.dataSource,
            });
          }
        }
      },
    });
  }
  UNSAFE_componentWillReceiveProps(nextPorps) {
    if (JSON.stringify(nextPorps.baseInfo) != JSON.stringify(this.props.baseInfo)) {
      this.setState({
        startDate: nextPorps.baseInfo.startDate,
        endDate: nextPorps.baseInfo.endDate,
        assetType: nextPorps.baseInfo.assetType,
      });
    }
  }
  //可供外部调用获取资产类基本信息
  getInfo = (callback) => {
    let values = this.state.baseInfoRef.getFormGroupData();
    if (values) {
      callback({
        ...values,
        startDate: values.startDate ? moment(values.startDate).format('YYYY-MM-DD') : '',
        endDate: values.endDate ? moment(values.endDate).format('YYYY-MM-DD') : '',
        fixedRate: values.fixedRate != null && values.fixedRate != undefined ? values.fixedRate / 100 : undefined,
        parTotalValue: values.parTotalValue ? Number(values.parTotalValue) : undefined,
        spareFaceValue: values.spareFaceValue ? Number(values.spareFaceValue) : undefined,
        issueScale: values.issueScale ? Number(values.issueScale) : undefined,
        jgbb_fdyz: values?.jgbb_fdyz || values.jgbb_fdyz == 0 ? values.jgbb_fdyz / 100 : null,
      });
    } else {
      message.error('请填写完表单再提交！');
      return false;
    }
    // this.state.baseInfoRef.props.form.validateFieldsAndScroll((err, values) => {
    //     if (err) {
    //         message.error('请填写完表单再提交！');
    //         return false;
    //     } else {
    //         callback({
    //             ...values,
    //             startDate: values.startDate ? moment(values.startDate).format('YYYY-MM-DD') : '',
    //             endDate: values.endDate ? moment(values.endDate).format('YYYY-MM-DD') : '',
    //             fixedRate: values.fixedRate != null && values.fixedRate != undefined ? values.fixedRate / 100 : undefined,
    //             parTotalValue:values.parTotalValue?Number(values.parTotalValue):undefined,
    //             spareFaceValue:values.spareFaceValue?Number(values.spareFaceValue):undefined,
    //             issueScale:values.issueScale?Number(values.issueScale):undefined,

    //         });
    //     }
    // })
  };

  render() {
    const { startDate, endDate, assetType, rateType } = this.state;
    const { disableStatus, baseInfo = {}, nonstandarmaintenanceNewM, baseReportChangeBaseInfo, pageType } = this.props;
    const { dictMapSelect } = nonstandarmaintenanceNewM;
    const disabled = disableStatus;
    return (
      <>
        <FormGroup
          rowNumber={3}
          autoLayout={false}
          dataSource={[
            {
              dataIndex: 'assetType',
              title: '资产类别',
              type: DATA_ENTRY.SELECT,
              initialValue: baseInfo.assetType,
              disabled,
              allowClear: true,
              dataSource: dictMapSelect['ASSETS_ASSET_TYPE'] || [],
              rules: [
                {
                  required: true,
                  message: '请选择资产类别',
                },
              ],
              onChange: (e) => {
                this.props?.onBaseInfoChange({ assetType: e }), this.setState({ assetType: e });
              },
            },
            {
              dataIndex: 'tradingcode',
              title: '证券代码',
              type: DATA_ENTRY.INPUT,
              initialValue: baseInfo.tradingcode,
              disabled: pageType == 'edit' ? true : disabled,
              rules: [
                {
                  required: true,
                  message: '请输入证券代码',
                },
              ],
            },
            {
              dataIndex: 'tradingname',
              title: '证券全称',
              type: DATA_ENTRY.INPUT,
              initialValue: baseInfo.tradingname,
              disabled: disabled,
              rules: [{ required: true, message: '请输入证券名称' }],
            },
            {
              dataIndex: 'secuabbr',
              title: '证券简称',
              type: DATA_ENTRY.INPUT,
              initialValue: baseInfo.secuabbr,
              disabled: disabled,
              rules: [{ required: true, message: '请输入证券简称' }],
            },
            {
              dataIndex: 'parTotalValue',
              title: '初始份额面值',
              type: DATA_ENTRY.INPUTNUMBER,
              initialValue: baseInfo.parTotalValue,
              unit: '元',
              splitStyle: true,
              precision: 4,
              disabled,
              min: 0,
              max: 999999999999,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ parTotalValue: e });
              },
              rules: [{ required: true, message: '请输入份额面值' }], //【资产类别】为“同业借款”、“收益凭证”、“债权投资计划和资产支持计划”时，必填
            },
            {
              dataIndex: 'issueScale',
              title: '发行规模',
              type: DATA_ENTRY.INPUTNUMBER,
              initialValue: baseInfo.issueScale,
              unit: '元',
              precision: 2,
              splitStyle: true,
              disabled,
              min: 0,
              max: 9999999999,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ issueScale: e });
              },
            },
            {
              dataIndex: 'issuer',
              title: '融资人',
              type: DATA_ENTRY.INPUT,
              initialValue: baseInfo.issuer,
              max: 200,
              disabled,
              rules: [{ required: true, message: '请输入融资人' }],
            },
            {
              dataIndex: 'currency',
              title: '币种',
              type: DATA_ENTRY.INPUT,
              initialValue: baseInfo.currency || 'CNY',
              dataSource: dictMapSelect['INVEST_CURRENCY'] || [],
              disabled: true,
              rules: [{ required: true, message: '请选择币种' }],
            },
            {
              dataIndex: 'startDate',
              title: '起息日',
              type: DATA_ENTRY.SINGLEDATE,
              initialValue: baseInfo.startDate ? moment(baseInfo.startDate) : null,
              disableEnd: endDate ? moment(endDate) : '',
              disabled,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ startDate: e }), this.setState({ startDate: e ? e.format('YYYY-MM-DD') : e });
              },
              rules: [{ required: true, message: '请选择起息日' }],
            },
            {
              dataIndex: 'endDate',
              title: '到期日',
              type: DATA_ENTRY.SINGLEDATE,
              initialValue: baseInfo.endDate ? moment(baseInfo.endDate) : null,
              disableBegin: startDate ? moment(startDate) : '',
              disabled,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ endDate: e }), this.setState({ endDate: e ? e.format('YYYY-MM-DD') : e });
              },
              rules: [{ required: true, message: '请选择到期日' }],
            },
            {
              dataIndex: 'rateType',
              title: '利率类型',
              type: DATA_ENTRY.SELECT,
              initialValue: baseInfo.rateType,
              disabled,
              dataSource: dictMapSelect['ASSETS_RATE_TYPE'] || [],
              rules: [{ required: true, message: '请选择利率类型' }],
              onChange: (e) => {
                this.props?.onBaseInfoChange({ rateType: e }), this.setState({ rateType: e });
              },
            },
            {
              dataIndex: 'fixedRate',
              title: '利率',
              type: DATA_ENTRY.INPUTNUMBER,
              initialValue: baseInfo.fixedRate != null && baseInfo.fixedRate != undefined ? baseInfo.fixedRate * 100 : null,
              unit: '%',
              precision: 5,
              disabled: disabled,
              min: 0,
              max: 99,
              hide: rateType == undefined ? baseInfo.rateType != 0 : rateType != 0,
              rules: [{ required: true, message: '请输入利率' }],
              onChange: (e) => {
                this.props?.onBaseInfoChange({ fixedRate: e != null && e != undefined ? e / 100 : undefined });
              },
            },
            {
              dataIndex: 'interestBasis',
              title: '计息基础',
              type: DATA_ENTRY.SELECT,
              initialValue: baseInfo.interestBasis,
              disabled,
              dataSource: dictMapSelect['ASSETS_INTEREST_BASIS'] || [],
              rules: [{ required: true, message: '请选择计息基础' }],
              hide: rateType == undefined ? baseInfo.rateType != 0 : rateType != 0,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ interestBasis: e });
              },
            },
            {
              dataIndex: 'interestRate',
              title: '计息频率',
              type: DATA_ENTRY.INPUTNUMBER,
              initialValue: baseInfo.interestRate,
              unit: '次/年',
              disabled,
              min: 0,
              precision: 0,
              max: 99,
              rules: [{ required: true, message: '请输入计息频率' }],
              hide: rateType == undefined ? baseInfo.rateType != 0 : rateType != 0,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ interestRate: e });
              },
            },
            {
              dataIndex: 'jgbb_jzllzl', //“项目收益率”“基准利率种类”不得同时为空
              title: '基准利率',
              type: DATA_ENTRY.SELECT,
              dataSource: dictMapSelect['ASSETS_JZLLZL'],
              initialValue: baseInfo?.jgbb_jzllzl,
              allowClear: true,
              disabled,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ jgbb_jzllzl: e });
              },
              hide: rateType == undefined ? baseInfo.rateType != 1 : rateType != 1,
              rules: [{ required: true, message: '请选择基准利率' }],
            },
            {
              dataIndex: 'jgbb_fdyz',
              title: '浮动因子',
              type: DATA_ENTRY.INPUTNUMBER,
              precision: 5,
              unit: 'bp',
              disabled,
              initialValue: baseInfo?.jgbb_fdyz || (baseInfo && baseInfo.jgbb_fdyz == 0) ? baseInfo?.jgbb_fdyz * 100 : null,
              hide: rateType == undefined ? baseInfo.rateType != 1 : rateType != 1,
              rules: [{ required: true, message: '请输入浮动因子' }],
            },
            {
              dataIndex: 'mainRatingStart',
              title: '融资人主体评级 ',
              type: DATA_ENTRY.SELECT,
              allowClear: true,
              initialValue: baseInfo.mainRatingStart,
              disabled,
              dataSource: dictMapSelect['ASSETS_MAIN_RATING_START'] || [],
            },
            {
              dataIndex: 'mainRatingDepart',
              title: '主体信用评级机构 ',
              type: DATA_ENTRY.SELECT,
              initialValue: baseInfo.mainRatingDepart,
              disabled,
              dataSource: this.state.mainRatingDepartData,
              filterOption: (
                input,
                option, //模糊搜索value 和 title
              ) => {
                return option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0 || option.props.value.toLowerCase().indexOf(input.toLowerCase()) >= 0;
              },

              showSearch: true,
              allowClear: true,
            },
            {
              dataIndex: 'debtRatingStart',
              title: '资产评级',
              type: DATA_ENTRY.SELECT,
              allowClear: true,
              initialValue: baseInfo.debtRatingStart,
              disabled,
              dataSource: dictMapSelect['ASSETS_MAIN_RATING_START'] || [],
            },
            {
              dataIndex: 'debtRatingDepart',
              title: '资产信用评级机构 ',
              type: DATA_ENTRY.SELECT,
              initialValue: baseInfo.debtRatingDepart,
              disabled,
              dataSource: this.state.debtRatingDepartData,
              filterOption: (
                input,
                option, //模糊搜索value 和 title
              ) => {
                return option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0 || option.props.value.toLowerCase().indexOf(input.toLowerCase()) >= 0;
              },
              showSearch: true,
              allowClear: true,
            },
            {
              dataIndex: 'guaranteeWay',
              title: '担保方式',
              type: DATA_ENTRY.SELECT,
              initialValue: baseInfo.guaranteeWay,
              disabled,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ guaranteeWay: e });
              },
              dataSource: dictMapSelect['ASSETS_GUARANTEE_WAY'] || [],
            },
            {
              dataIndex: 'jgbb_hqlx1',
              title: '含权类型',
              type: DATA_ENTRY.SELECT,
              dataSource: dictMapSelect['ASSETS_HQLX1'],
              initialValue: baseInfo?.jgbb_hqlx1,
              allowClear: true,
              disabled,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ jgbb_hqlx1: e });
              },
            },
            {
              dataIndex: 'jgbb_ssyqlx1',
              title: '收/受益权类型',
              type: DATA_ENTRY.SELECT,
              initialValue: baseInfo?.jgbb_ssyqlx1,
              dataSource: dictMapSelect['NONSTANDAR_JGBB_SSYQLX1'],
              disabled,
              onChange: (e) => {
                this.props?.onBaseInfoChange({ jgbb_ssyqlx1: e });
              },
              hide: assetType != '2',
            },
            {
              dataIndex: 'jgbb_txbffl',
              title: '投向部分分类',
              type: DATA_ENTRY.SELECT,
              dataSource: dictMapSelect['ASSETS_TXBFFL'],
              initialValue: baseInfo?.jgbb_txbffl,
              disabled,
              hide: assetType != '2',
            },
          ]}
          onRef={(ref) => {
            this.state.baseInfoRef = ref;
          }}
        ></FormGroup>
      </>
    );
  }
}

// props默认值
ZcBaseInfo.propsTypes = {
  onRef: () => {}, // 组件this
  disableStatus: PropTypes.string, // 是否是查看得状态 true/false
  pageType: PropTypes.string, // 页面类型 add: '新增',edit：'编辑'
  baseBaseInfoChangeReport: PropTypes.object,
  baseInfo: PropTypes.object, // 编辑时必传，基础信息
};
export default connect(({ nonstandarmaintenanceNewM }) => ({
  nonstandarmaintenanceNewM,
}))(ZcBaseInfo);
